package com.hqyj.springBoot.modules.test.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hqyj.springBoot.modules.common.vo.Result;
import com.hqyj.springBoot.modules.common.vo.Search;
import com.hqyj.springBoot.modules.test.dao.CityDao;
import com.hqyj.springBoot.modules.test.dao.CityDao2;
import com.hqyj.springBoot.modules.test.entity.City;
import com.hqyj.springBoot.modules.test.service.CityService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Optional;

@Service
public class CityServiceImpl implements CityService {

    @Autowired
    private CityDao cityDao;

    @Autowired
    private CityDao2 cityDao2;

    @Override
    @Transactional
    public Result<City> insertCity(City city) {
        City temp = cityDao.getCityByCityName(city.getCityName());
        if (temp != null){
            return new Result<>(Result.ResultStatus.FAILED.code, "City name is repeat");
        }

        city.setDateCreated(new Date());
        city.setDateModified(new Date());

//        cityDao.insertCity(city);
        cityDao2.insert(city);

        return new Result<>(Result.ResultStatus.SUCCESS.code, "Insert success",city);
    }



    @Transactional
    @Override
    public Result<City> updateCity(City city) {

        City temp = cityDao.getCityByCityName(city.getCityName());
        if (temp != null && temp.getCityId() != city.getCityId()){
            return new Result<>(Result.ResultStatus.FAILED.code, "City name is repeat");
        }
        city.setDateModified(new Date());

//        cityDao.updateCity(city);
        UpdateWrapper<City> wrapper = new UpdateWrapper<>();

        wrapper.set(StringUtils.isNotBlank(city.getCityName()),//为true,也就是cityName不为空才修改
                "city_name",//列名
                city.getCityName());
        wrapper.set(StringUtils.isNotBlank(city.getLocalCityName()),//为true,也就是cityName不为空才修改
                "local_city_name",//列名
                city.getLocalCityName());
        wrapper.eq(true,"city_id",city.getCityId());

        cityDao2.update(city,wrapper);


        return new Result<>(Result.ResultStatus.SUCCESS.code, "Update success",city);
    }

    @Override
    @Transactional
    public Result<Object> deleteCity(int cityId) {
//        cityDao.deleteCity(cityId);
        cityDao2.deleteById(cityId);
        return new Result<>(Result.ResultStatus.SUCCESS.code, "Delete success");
    }

    @Override
    @Transactional
    public City getCityByCityId(int cityId) {
//        return cityDao.getCityByCityId(cityId);
        return cityDao2.selectById(cityId);
    }

    @Override
    @Transactional
    public List<City> getCitiesByCountryId(int countryId) {
        return Optional.ofNullable(cityDao.getCitiesByCountryId(countryId))
                .orElse(Collections.emptyList());

    }

    @Override
    @Transactional
    public PageInfo<City> getCitiesBySearch(Search search) {
        //初始化search
        search.init();
        //开启分页
        PageHelper.startPage(search.getCurrentPage(),search.getPageSize());
        return PageInfo.of(Optional.ofNullable(cityDao.getCitiesBySearch(search))
                            .orElse(Collections.emptyList()));
    }

    @Override
    public Page<City> getCitiesBySearch2(Search search) {
        search.init();

        Page page = new Page(search.getCurrentPage(),search.getPageSize());

        QueryWrapper<City> wrapper = new QueryWrapper<>();
        wrapper.like(
                StringUtils.isNotBlank(search.getKeyword()),
                "city_name",
                search.getKeyword()
        ).or().like(
                StringUtils.isNotBlank(search.getKeyword()),
                "local_city_name",
                search.getKeyword()
        ).orderBy(
                true,
                StringUtils.isBlank(search.getDirection()) ||
                        search.getDirection().toLowerCase().equals("asc"),
                StringUtils.isBlank(search.getSort()) ? "city_id" : search.getSort()
        );//条件、是否正序、排序条件

        return cityDao2.selectPage(page,wrapper);

    }
}
